/**
 * 包含n个reducer函数： 根据旧的state和指定的action返回一个新的state
 */
import {combineReducers} from 'redux'
import { 
        AUTH_SUCCESS,
        ERROR_MSG, 
        RESET_USER, 
        RECEIVE_USER,
        RECEIVE_USER_LIST
    } from './action-types'
import { getRedirectTo } from '../utils'

const initUser = {
    username: '',
    type: '',
    msg: '',    // 存放错误提示信息
    redirectTo: ''  // 指定需要重定向的路由路径
}
// 产生user状态的reducer
function user(state=initUser, action){
    switch(action.type){
        case AUTH_SUCCESS:
            const {type, header} = action.data
            return {...action.data, redirectTo: getRedirectTo(type, header)}
        case ERROR_MSG:
            return {...state, msg: action.data}
        case RECEIVE_USER:
            return action.data  // action.data 是user
        case RESET_USER:
            return { ...initUser, msg: action.msg}  // action.data是msg
        default:
            return state
    }
}

const initUserList = []
function userList(state=initUserList, action){
    switch (action.type){
        case RECEIVE_USER_LIST:
            return action.data
        default :
            return state 
    }
}

export default combineReducers({
    user,
    userList
})